package util;

import java.sql.*;

/**
 *
 * @author lzj
 *
 */
public class DbUtil {

    /**常量命名应该全部大写，单词间用下划线隔开*/
    private static final String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String DB_URL = "jdbc:mysql://localhost:3306/tcmsystem?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "123aaa";

    public DbUtil() {
    }

    static {
        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn,Statement stmt,ResultSet rs){
        try{
            if(rs!=null) {
                rs.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        try{
            if(stmt!=null) {
                stmt.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        try{
            if(conn!=null) {
                conn.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
